import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xvideos/alert_business/video/video_buy_bottom_sheet.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_page/mine_business/mine_vip/mine_charge_vip_page.dart';
import 'package:xvideos/hj_utils/api_service/vid_service.dart';
import 'package:xvideos/hj_utils/history_record_util.dart';
import 'package:xvideos/tools_base/widget/scroll_positioned_list/src/item_positions_listener.dart';
import 'package:xvideos/tools_base/widget/scroll_positioned_list/src/scrollable_positioned_list.dart';

class PicsDetailLogic extends GetxController {
  final String? id;
  VideoModel? videoModel;
  ValueNotifier<int> _indexNotifier = ValueNotifier<int>(0); //局部刷新
  final itemPositionsListener = ItemPositionsListener.create();
  final itemScrollCtr = ItemScrollController();

  PicsDetailLogic(this.id);

  onReady() {
    super.onReady();
    _indexNotifier.value = 0;
    _loadData();
  }

  void reloadData() {
    _loadData();
  }

  Future _loadData() async {
    videoModel = await VidService.getVideoDetail(id ?? "");
    if (videoModel != null) {
      HistoryRecordUtil.insertHistoryModel<VideoModel>(videoModel!, HistoryRecordType.picture);
    }
    videoModel ??= VideoModel();
    update();
  }

  //解锁
  showBuyAction() async {
    //1-金币解锁 2-会员解锁
    if (videoModel?.picLockType == 1) {
      final res = await Get.bottomSheet(VideoBuyBottomSheet(videoM: videoModel!));
      if (res == true) {
        videoModel?.vidStatus?.hasPaid = true;
        update();
      }
    } else if (videoModel?.picLockType == 2) {
      Get.to(MineChargeVipPage(), preventDuplicates: false);
    }
  }
}
